package com.sourcetrip.raw.model;

/**
 * @author: ZhouBert
 * @date: 2020/11/24
 * @description:	二叉搜索树的节点
 */
public class MyBinaryNode<E> {

	/**
	 * 节点中元素的值
	 */
	protected E element;
	/**
	 * 父节点
	 * 除了根节点，必有
	 */
	protected MyBinaryNode<E> parent;


	/**
	 * 左子树节点
	 */
	protected MyBinaryNode<E> left;
	/**
	 * 右子树节点
	 */
	protected MyBinaryNode<E> right;


	/**
	 * constructor
	 * @param element
	 * @param parent
	 */
	public MyBinaryNode(E element, MyBinaryNode<E> parent) {
		this.element = element;
		this.parent = parent;
	}

	public E getElement() {
		return element;
	}

	public void setElement(E element) {
		this.element = element;
	}

	public MyBinaryNode<E> getParent() {
		return parent;
	}

	public void setParent(MyBinaryNode<E> parent) {
		this.parent = parent;
	}

	public MyBinaryNode<E> getLeft() {
		return left;
	}

	public void setLeft(MyBinaryNode<E> left) {
		this.left = left;
	}

	public MyBinaryNode<E> getRight() {
		return right;
	}

	public void setRight(MyBinaryNode<E> right) {
		this.right = right;
	}

	/**
	 * 是否为 父节点的 左节点
	 *
	 * @return
	 */
	public boolean isLeftChild() {
		return parent != null && parent.left == this;
	}

	/**
	 * 是否为 父节点的 右节点
	 * @return
	 */
	public boolean isRightChild() {
		return parent != null && parent.right == this;
	}

	/**
	 * 判断该节点是否为叶子节点
	 * @return
	 */
	public boolean isLeafNode(){
		return this.getLeft() == null && this.getRight() == null;
	}

	/**
	 * 只打印 element 的值
	 * @return
	 */
	@Override
	public String toString() {
		return element.toString();
	}
}
